package com.atguigu.juc_day02;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * java
 * 2022/9/8 19 18
 * 对一个初始值为0的变量实现+1 -1 交替进行10论
 */
class AirCondition{
    private int number = 0;
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    public void increment() throws InterruptedException {
        lock.lock ();

            try {
                while (number!=0){
                   condition.await ();
                }
                System.out.println (Thread.currentThread ().getName ()+"******"+number);
                number++;
                condition.signalAll ();

            } finally {
                lock.unlock ();
            }
    }
    public void decrement() throws InterruptedException {
        lock.lock ();
        try {
            while (number==0){
                condition.await ();
            }
            System.out.println (Thread.currentThread ().getName ()+"********"+number);
            number--;
            condition.signalAll ();

        }  finally {
            lock.unlock ();

        }
    }


    /**
     * 使用Synchronization的旧版
     */
   /* public synchronized void increment() throws InterruptedException {
        while (number!=0){
            wait ();
        }
        System.out.println (Thread.currentThread ().getName ()+"+1，目前为"+number);
        number++;
        notifyAll ();
    }
    public synchronized void decrement() throws InterruptedException {
        while (number==0){
            wait ();
        }
        System.out.println (Thread.currentThread ().getName ()+"-1，目前为"+number);
        number--;
        notifyAll ();
    }*/
    /**
     * 异常版本
     */
   /* public synchronized void increment() throws InterruptedException {
        if (number!=0){
            wait ();
        }
        System.out.println (Thread.currentThread ().getName ()+"+1，目前为"+number);
        number++;
        notify ();
    }
    public synchronized void decrement() throws InterruptedException {
        if (number==0){
            wait ();
        }
        System.out.println (Thread.currentThread ().getName ()+"-1，目前为"+number);
        number--;
        notify ();
    }*/
}
public class ProdConsumerDemo {
    public static void main (String[] args) {
        //thread2 ();
        AirCondition airCondition = new AirCondition ();
        new Thread (()->{
            for (int i = 0; i < 20; i++) {
                try {
                    airCondition.increment ();
                } catch (Exception e) {
                    throw new RuntimeException (e);
                }
            }
        },"A").start ();
        new Thread (()->{
            for (int i = 0; i < 20; i++) {
                try {
                    airCondition.decrement();
                } catch (Exception e) {
                    throw new RuntimeException (e);
                }
            }
        },"B").start ();
        new Thread (()->{
            for (int i = 0; i < 20; i++) {
                try {
                    airCondition.increment ();
                } catch (Exception e) {
                    throw new RuntimeException (e);
                }
            }
        },"C").start ();
        new Thread (()->{
            for (int i = 0; i < 20; i++) {
                try {
                    airCondition.decrement();
                } catch (Exception e) {
                    throw new RuntimeException (e);
                }
            }
        },"D").start ();
    }

    /**
     * 两个线程的调用
     */
    private static void thread2 () {
        AirCondition airCondition = new AirCondition ();
        new Thread (()->{
            for (int i = 0; i < 10; i++) {
                try {
                    airCondition.increment ();
                } catch (Exception e) {
                    throw new RuntimeException (e);
                }
            }
        },"A").start ();
        new Thread (()->{
            for (int i = 0; i < 10; i++) {
                try {
                    airCondition.decrement();
                } catch (Exception e) {
                    throw new RuntimeException (e);
                }
            }
        },"B").start ();
    }
}
